import { OBJLoader } from '../../lib/loaders/OBJLoader.js';
import { MTLLoader } from '../../lib/loaders/MTLLoader.js';

const objLoader = function () {
  if (this.resourcePath) {
    const mtlLoader = new MTLLoader();
    mtlLoader.load(this.resourcePath, (materials) => {
      materials.preload();
      const loader = new OBJLoader();
      loader.setMaterials(materials);
      loader.load(
        this.modelPath,
        (object) => this.publicLoadCallback(object, loader.fileSize),
        this.loadProgress.bind(this),
      );
    });
  } else {
    const loader = new OBJLoader();
    if (this.resourcePath) {
      loader.setResourcePath(this.resourcePath);
    }
    loader.load(
      this.modelPath,
      (object) => this.publicLoadCallback(object, loader.fileSize),
      this.loadProgress.bind(this),
    );
  }
};

export default objLoader;
